﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using Microsoft.SystemCenter.Orchestrator.Integration;
using System.Management;
using sccmclictr.automation;

namespace OpalisAddOn.DDR
{
    [Activity("New Computer DDR", Description = "Create DDRFile content to create a new Computer", ShowFilters = false, ShowInputs = true)]
    public class NewComputerDDR : IActivity
    {
        public void Design(IActivityDesigner designer)
        {
            designer.AddInput("SiteCode");
            designer.AddInput("Computer Name");
            designer.AddInput("TargetFile").WithFileBrowser();
            designer.AddInput("MAC Address").NotRequired();
            designer.AddInput("SMBIOS GUID").NotRequired();

            //designer.AddInput("AD Site Name").NotRequired();
            //designer.AddInput("Location").NotRequired();
            //designer.AddInput("OS").NotRequired();
            //designer.AddInput("Role").NotRequired();
            designer.AddInput("CreateNewGUID").NotRequired().WithBooleanBrowser();

            //Swisscom Settings
            /*designer.AddInput("SCS-BootGroup").NotRequired();
            designer.AddInput("SCS-OSEdition").NotRequired().WithListBrowser(new object[] { "Standard", "Enterprise" }); ;
            designer.AddInput("SCS-MachineType").NotRequired();
            designer.AddInput("SCS-AssetNumber").NotRequired();
            designer.AddInput("SCS-DeviceType").NotRequired().WithListBrowser(new object[] { "Virtual Machine", "Physical Server" });
            designer.AddInput("OS").NotRequired().WithListBrowser(new object[] { "WIN2008", "WIN2008R2", "WIN2012" });
             */

        }
        
        public void Execute(IActivityRequest request, IActivityResponse response)
        {
            sccmclictr.automation.DDRFile newDDR =  new DDRFile("System", "Opalis Discovery", request.Inputs["SiteCode"].AsString().ToUpper());
            
            newDDR.DDRAddString("Name", request.Inputs["Computer Name"].AsString(), 16, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NAME | DDRFile.DDRPropertyFlagsEnum.ADDPROP_KEY);
            newDDR.DDRAddString("Netbios Name", request.Inputs["Computer Name"].AsString(), 16, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NAME);
            newDDR.DDRAddStringArray("SMS Assigned Sites", new string[] { request.Inputs["SiteCode"].AsString().ToUpper() }, 16, DDRFile.DDRPropertyFlagsEnum.ADDPROP_REPLACE);
            newDDR.DDRAddStringArray("SMS Installed Sites", new string[] { request.Inputs["SiteCode"].AsString().ToUpper() }, 16, DDRFile.DDRPropertyFlagsEnum.ADDPROP_REPLACE);
            
            if(request.Inputs.Contains("CreateNewGUID"))
            {
                if (Boolean.Parse(request.Inputs["CreateNewGUID"].AsString()))
                {
                    newDDR.DDRAddString("SMS Unique Identifier", "GUID:" + System.Guid.NewGuid().ToString(), 42, DDRFile.DDRPropertyFlagsEnum.ADDPROP_GUID | DDRFile.DDRPropertyFlagsEnum.ADDPROP_KEY);
                }
            }

            if (request.Inputs.Contains("MAC Address"))
            {
                string sMAC = request.Inputs["MAC Address"].AsString();

                if (sMAC.Length == 12)
                {
                    sMAC = sMAC.Insert(2, "-");
                    sMAC = sMAC.Insert(5, "-");
                    sMAC = sMAC.Insert(8, "-");
                    sMAC = sMAC.Insert(11, "-");
                    sMAC = sMAC.Insert(14, "-");
                }
                sMAC = sMAC.Replace('-', ':');
                sMAC = sMAC.ToUpper();

                newDDR.DDRAddStringArray("MAC Addresses", new string[] { sMAC }, 64, DDRFile.DDRPropertyFlagsEnum.ADDPROP_KEY);

            }

            //Assign Optional strings...
            if (request.Inputs.Contains("AD Site Name"))
                newDDR.DDRAddString("AD Site Name", request.Inputs["AD Site Name"].AsString(), 64, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NONE);
            if (request.Inputs.Contains("Location"))
                newDDR.DDRAddString("Location", request.Inputs["Location"].AsString(), 64, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NONE);
            if (request.Inputs.Contains("OS"))
                newDDR.DDRAddString("Operating System Name and Version", request.Inputs["OS"].AsString(), 64, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NONE);
            if (request.Inputs.Contains("Role"))
                newDDR.DDRAddString("Role", request.Inputs["Role"].AsString(), 64, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NONE);
            if (request.Inputs.Contains("SMBIOS GUID"))
                newDDR.DDRAddString("SMBIOS GUID", request.Inputs["SMBIOS GUID"].AsString(), 38, DDRFile.DDRPropertyFlagsEnum.ADDPROP_KEY);

            //Swisscom Settings
            if (request.Inputs.Contains("SCS-BootGroup"))
                newDDR.DDRAddString("SCS-BootGroup", request.Inputs["SCS-BootGroup"].AsString(), 64, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NONE);
            if (request.Inputs.Contains("SCS-OSEdition"))
                newDDR.DDRAddString("SCS-OSEdition", request.Inputs["SCS-OSEdition"].AsString(), 64, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NONE);
            if (request.Inputs.Contains("SCS-MachineType"))
                newDDR.DDRAddString("SCS-MachineType", request.Inputs["SCS-MachineType"].AsString(), 64, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NONE);
            if (request.Inputs.Contains("SCS-AssetNumber"))
                newDDR.DDRAddString("SCS-AssetNumber", request.Inputs["SCS-AssetNumber"].AsString(), 64, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NONE);
            if (request.Inputs.Contains("SCS-DeviceType"))
                newDDR.DDRAddString("SCS-DeviceType", request.Inputs["SCS-DeviceType"].AsString(), 64, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NONE);
            

            newDDR.DDRAddDateTime("SCS-RequestTimeStamp", DateTime.Now, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NONE);
            
            //Save DDR File
            newDDR.DDRWrite(request.Inputs["TargetFile"].AsString());
        }
    }
}
